/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session.stateless;

import entity.RightEntity;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import entity.SystemUserEntity;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import javax.faces.model.SelectItem;
import javax.annotation.security.PermitAll;
import javax.persistence.Query;

/**
 *
 * @author wei
 * DO NOT use this session bean anymore. 
 * Any method relevant to the users please put under SystemUserEntityFacade!
 */
@Stateless
public class SystemUserSessionBean {

    @PersistenceContext
    private EntityManager entityManager;

    public SystemUserEntity getSystemUserById(Long id) {
        return entityManager.find(SystemUserEntity.class, id);
    }

    
    public List<SystemUserEntity> getSystemUsersByName(String userName) {
        Query query = entityManager.createQuery(
                "SELECT u FROM SystemUser u WHERE u.name = :inUserName");
        query.setParameter("inUserName", userName);
        List<SystemUserEntity> searchResults = query.getResultList();

        return searchResults;
}
    
  
    
    
    public SystemUserEntity getSystemUserByName(String userName) {
        Query query = entityManager.createQuery(
                "SELECT u FROM SystemUser u WHERE u.name = :inUserName");
        query.setParameter("inUserName", userName);
        SystemUserEntity searchResults = (SystemUserEntity) query.getSingleResult();
return searchResults;
    }
    public List<SystemUserEntity> getAllSystemUsers() {
        Query query = entityManager.createQuery("SELECT s FROM SystemUser s");
        List res = query.getResultList();
        System.out.println(res.size());
        return res;
    }

    public List<SelectItem> getSystemUserDropList() {
        List<SelectItem> systemUserList = new ArrayList();
        Query query = entityManager.createQuery("SELECT s FROM SystemUser s");
        for (Object o : query.getResultList()) {
            SystemUserEntity systemUser = (SystemUserEntity) o;
            SelectItem selectItem = new SelectItem(systemUser.getId(), Long.toString(systemUser.getId()));
            systemUserList.add(selectItem);
        }
        return systemUserList;
    }

    @PermitAll
    public SystemUserEntity createOrUpdateSystemUser(SystemUserEntity user) {
        System.out.println(user);

        SystemUserEntity savedUser;
        savedUser = entityManager.merge(user);

        entityManager.flush();
        return savedUser;
    }
//    public Long login(String userName, String password) {
//        SystemUser systemUser = getSystemUserByName(userName);
//        if (systemUser == null) {
//            return null;
//        } else {           
//            if (systemUser.getPassword().equals(encryptPassword(password))) {
//                return systemUser.getId();
//            } else {
//                return null;
//            }
//        }
//    }
}
